#some code to give rsvg.render_cairo(ctx) ability
#on windows.
import os,cairo,pyglet
try:
    import rsvg
    WINDOWS=False
except ImportError:
    print"Warning, could not import 'rsvg'"
    if os.name == 'nt':
        print "Detected windows, creating rsvg."
        #some workarounds for windows

        from ctypes import *

        l=CDLL('librsvg-2-2.dll')
        g=CDLL('libgobject-2.0-0.dll')
        g.g_type_init()

        class rsvgHandle():
            class RsvgDimensionData(Structure):
                _fields_ = [("width", c_int),
                            ("height", c_int),
                            ("em",c_double),
                            ("ex",c_double)]

            class PycairoContext(Structure):
                _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                            ("ctx", c_void_p),
                            ("base", c_void_p)]

            def __init__(self, path):
                self.path = path
                error = ''
                self.handle = l.rsvg_handle_new_from_file(self.path,error)


            def get_dimension_data(self):
                svgDim = self.RsvgDimensionData()
                l.rsvg_handle_get_dimensions(self.handle,byref(svgDim))
                return (svgDim.width,svgDim.height)

            def render_cairo(self, ctx):
                ctx.save()
                z = self.PycairoContext.from_address(id(ctx))
                l.rsvg_handle_render_cairo(self.handle, z.ctx)
                ctx.restore()
                
            def render_sized(self,ctx,width,height):
                w,h = self.get_dimension_data()
                ctx.stroke_preserve()
                scale = float(width)/float(w),float(height)/float(h)
                ctx.set_line_width(0.001)
                ctx.scale(scale[0],scale[1])
                self.render_cairo(ctx)



        class rsvgClass():
            def Handle(self,file):
                return rsvgHandle(file)

        rsvg = rsvgClass()


class SVG_Render:
    def __init__(self):
        self.dict = {}
    def isLoaded(self,or_name,width,height):
        return (str(width)+'_'+str(height)+'_'+or_name.split('.')[0]+'.png') in self.dict
    def getImage(self,or_name,width,height):
        
        name = or_name.replace('/','')
        name = str(width)+'_'+str(height)+'_'+name.split('.')[0]+'.png'
        if name not in self.dict:
            h = rsvg.Handle(or_name)
            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
            ctx = cairo.Context(surface)
            bg = (1.0,1.0,1.0)
            ctx.set_source_rgb(bg[0],bg[1],bg[2])
            ctx.paint_with_alpha(0.0)
            #h.render_sized(ctx,width,height)
            rsvgr = rsvg.Handle(or_name)
            w =  rsvgr.get_dimension_data()[0]
            h =  rsvgr.get_dimension_data()[1]
            
            ctx.stroke_preserve()
            scale = float(width)/float(w),float(height)/float(h)
            ctx.set_line_width(0.001)
            ctx.scale(scale[0],scale[1])
            rsvgr.render_cairo(ctx)
            surface.write_to_png('./tmp/'+name)
            self.dict[name] = pyglet.image.load('./tmp/'+name)
        
        return self.dict[name]
    
#~ h = rsvg.Handle("bar.svg")



#~ # Setup Cairo
#~ surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 200, 200)
#~ ctx = cairo.Context(surface)

#~ #bg
#~ bg = (1.0,1.0,1.0)
#~ ctx.set_source_rgb(bg[0],bg[1],bg[2])
#~ ctx.paint_with_alpha(0.0)

#~ h.render_sized(ctx,200,200)

#~ surface.write_to_png("test.png")
